import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/src/response.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';

class HttpRespose {
  dynamic data;
  Map<String, dynamic>? headers;

  HttpRespose({
    this.data,
    this.headers,
  });
}

class HttpApi {
  /**
   * @title get 请求
   */
  static Future<HttpRespose?> get({
    required String url,
    Map<String, String>? headers,
    Map<String, dynamic>? queryParameters,
    int timeout = 60,
  }) async {
    try {
      Response response = await http
          .get(Uri.parse(url).replace(queryParameters: queryParameters), headers: headers)
          .timeout(Duration(seconds: timeout));

      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        return HttpRespose(data: data, headers: response.headers);
      } else {
        FFLog.error("【请求接口报错】 $url\n【状态码】${response.statusCode}\n【错误信息】 ${response.reasonPhrase}\n");
      }
    } on TimeoutException catch (e) {
      FFLog.error("【请求接口报错】 $url\n【超时信息】 ${e.toString()}\n");
    } catch (e) {
      FFLog.error("【请求接口报错】 $url\n【错误信息】 ${e.toString()}\n");
    }

    return null;
  }

  /**
   * @title post 请求
   */
  static Future<HttpRespose?> post({
    required String url,
    Map<String, String>? headers,
    Map<String, dynamic>? queryParameters,
    Encoding? encoding,
    int timeout = 60,
  }) async {
    try {
      // headers ??= {};
      //headers["Content-Type"] = "application/json";

      final body = json.encode(queryParameters);
      Response response =
          await http.post(Uri.parse(url), headers: headers, body: body, encoding: encoding).timeout(Duration(seconds: timeout));

      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        return HttpRespose(data: data, headers: response.headers);
      } else {
        FFLog.error("【请求接口报错】 $url\n【状态码】${response.statusCode}\n【错误信息】 ${response.reasonPhrase}\n");
      }
    } on TimeoutException catch (e) {
      FFLog.error("【请求接口报错】 $url\n【超时信息】 ${e.toString()}\n");
    } catch (e) {
      FFLog.error("【请求接口报错catch】 $url\n【错误信息】 ${e.toString()}\n");
    }

    return null;
  }

  /**
   * @title put 请求
   */
  static Future<HttpRespose?> put({
    required String url,
    Map<String, String>? headers,
    Map<String, dynamic>? queryParameters,
    Encoding? encoding,
    int timeout = 60,
  }) async {
    try {
      // headers ??= {};
      //headers["Content-Type"] = "application/json";

      final body = json.encode(queryParameters);
      Response response =
      await http.put(Uri.parse(url), headers: headers, body: body, encoding: encoding).timeout(Duration(seconds: timeout));

      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        return HttpRespose(data: data, headers: response.headers);
      } else {
        FFLog.error("【请求接口报错】 $url\n【状态码】${response.statusCode}\n【错误信息】 ${response.reasonPhrase}\n");
      }
    } on TimeoutException catch (e) {
      FFLog.error("【请求接口报错】 $url\n【超时信息】 ${e.toString()}\n");
    } catch (e) {
      FFLog.error("【请求接口报错catch】 $url\n【错误信息】 ${e.toString()}\n");
    }

    return null;
  }

  /**
   * @title delete 请求
   */
  static Future<HttpRespose?> delete({
    required String url,
    Map<String, String>? headers,
    Map<String, dynamic>? queryParameters,
    Encoding? encoding,
    int timeout = 60,
  }) async {
    try {
      headers ??= {};
      // headers ??= {};
      //headers["Content-Type"] = "application/json";

      final body = json.encode(queryParameters);
      Response response =
          await http.delete(Uri.parse(url), headers: headers, body: body, encoding: encoding).timeout(Duration(seconds: timeout));

      if (response.statusCode == 200) {
        var data = json.decode(response.body);
        return HttpRespose(data: data, headers: response.headers);
      } else {
        FFLog.error("【请求接口报错】 $url\n【状态码】${response.statusCode}\n【错误信息】 ${response.reasonPhrase}\n");
      }
    } on TimeoutException catch (e) {
      FFLog.error("【请求接口报错】 $url\n【超时信息】 ${e.toString()}\n");
    } catch (e) {
      FFLog.error("【请求接口报错catch】 $url\n【错误信息】 ${e.toString()}\n");
    }

    return null;
  }

}
